#include "NetworkMenuView.h"

NetworkMenuView::NetworkMenuView(Screen *screen, Model *m, LPDIRECT3DDEVICE9 dd3d): View(screen, m, dd3d)
{
  _model = (NetworkMenuModel*)m;
  screen = screen;
  D3DXCreateFont(_dd3dDevice, 60, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET,
	             OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH || FF_DONTCARE,
				 "Arial", &_mainMenuFont);
  D3DXCreateFont(_dd3dDevice, 40, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET,
	             OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH || FF_DONTCARE,
				 "Arial", &_subMenuFont);
  InitRects();
}

void NetworkMenuView::InitRects(void)
{
  SetRect(&_titleRect, 100, 100, 1000, 200);
  SetRect(&_menuRect[0], 200, 200, 600, 300);
  SetRect(&_menuRect[1], 200, 300, 600, 400);
  SetRect(&_menuRect[2], 200, 400, 600, 500);
}

NetworkMenuView::~NetworkMenuView(void)
{
  _mainMenuFont->Release();
  _subMenuFont->Release();
}

void NetworkMenuView::Draw()
{
  HRESULT res;
  if(background != NULL)
  {
    GlobalDebug::GetInstance()->AssertAndLogResult(_sprite->Draw(this->background, NULL, NULL, NULL, D3DCOLOR_XRGB(255,255,255)), "NetworkMenuView::Draw background");
    _sprite->End();
    _sprite->Begin(D3DXSPRITE_ALPHABLEND);
  }
  res = _mainMenuFont->DrawTextA(NULL, _netMenuLabelText, strlen(_netMenuLabelText), &this->_titleRect, DT_CENTER | DT_VCENTER, D3DCOLOR_ARGB(255, 255, 255, 255));
	
  // figure out selected menu item
  int item = this->_model->GetSelectedMenuItem();

  res = _subMenuFont->DrawTextA(NULL, _menuServerModeOption, _menuServerModeLen, &this->_menuRect[0] , DT_LEFT | DT_VCENTER, item == 0 ? _netSelTextColor : _netNormalTextColor);	
  res = _subMenuFont->DrawTextA(NULL, _menuClientModeOption, _menuClientModeLen, &this->_menuRect[1], DT_LEFT | DT_VCENTER, item == 1 ? _netSelTextColor : _netNormalTextColor);
  res = _subMenuFont->DrawTextA(NULL, _menuBackOption, _menuBackLen, &this->_menuRect[2], DT_LEFT | DT_VCENTER, item == 2 ? _netSelTextColor : _netNormalTextColor);
}

void NetworkMenuView::HandleMessage(Message *msg)
{
  delete msg;
}

void NetworkMenuView::PreRender(void)
{
  GlobalDebug::GetInstance()->AssertAndLogResult(_sprite->Begin(D3DXSPRITE_ALPHABLEND), "View::PreRender Sprite::Begin");
}

void NetworkMenuView::PostRender(void)
{
  GlobalDebug::GetInstance()->AssertAndLogResult(_sprite->End(), "View::PostRender Sprite::End");
}
